package com.jiaotong.smart.smartjiaotong.service.GreenWaveService.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiaotong.smart.smartjiaotong.entity.GreenWave.*;
import com.jiaotong.smart.smartjiaotong.mapper.GreenWaveMapper.GreenWaveCrossMapper;
import com.jiaotong.smart.smartjiaotong.mapper.GreenWaveMapper.GreenWaveMapper;
import com.jiaotong.smart.smartjiaotong.service.GreenWaveService.GreenWaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.Long;
import java.util.List;

/**
 * @author 袁雨森
 * 绿波带操作service实现
 */
@Service
public class GreenWaveServiceImpl implements GreenWaveService {

    @Autowired
    GreenWaveMapper greenWaveMapper;
    @Autowired
    GreenWaveCrossMapper greenWaveCrossMapper;

    /**
     * 获取所有绿波带信息
     *
     * @return List<GreenWaveBand> 所有绿波带信息
     */
    @Override
    public ResAllGreenWave getAllGreenWave() {
        ResAllGreenWave resAllGreenWave = new ResAllGreenWave();

        List<GreenWave> greenWaveList = greenWaveMapper.selectList(null);
        Integer usingNumber = greenWaveMapper.getAllUsingBandNumber();
        Integer notUsingNumber = greenWaveMapper.getAllNotUsingBandNumber();

        if (usingNumber == null) {
            usingNumber = 0;
        }
        if (notUsingNumber == null) {
            notUsingNumber = 0;
        }

        resAllGreenWave.setGreenWaveList(greenWaveList);
        resAllGreenWave.setAllNumber(greenWaveList.size());
        resAllGreenWave.setUsingNumber(usingNumber);
        resAllGreenWave.setNotUsingNumber(notUsingNumber);

        return resAllGreenWave;
    }

    /**
     * 获取正在启用的绿波带信息
     *
     * @return List<GreenWaveBand> 启用的绿波带
     */
    @Override
    public ResAllGreenWave getAllUsingGreenWave() {
        ResAllGreenWave resAllGreenWave = new ResAllGreenWave();

        QueryWrapper<GreenWave> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<GreenWave> greenWaveList = greenWaveMapper.selectList(queryWrapper);

        Integer usingNumber = greenWaveMapper.getAllUsingBandNumber();
        Integer notUsingNumber = greenWaveMapper.getAllNotUsingBandNumber();

        if (usingNumber == null) {
            usingNumber = 0;
        }
        if (notUsingNumber == null) {
            notUsingNumber = 0;
        }

        resAllGreenWave.setGreenWaveList(greenWaveList);
        resAllGreenWave.setAllNumber(greenWaveList.size());
        resAllGreenWave.setUsingNumber(usingNumber);
        resAllGreenWave.setNotUsingNumber(notUsingNumber);

        return resAllGreenWave;
    }

    /**
     * 获取未启用的绿波带信息
     *
     * @return List<GreenWaveBand> 未启用的绿波带
     */
    @Override
    public ResAllGreenWave getAllNotUsingGreenWave() {
        ResAllGreenWave resAllGreenWave = new ResAllGreenWave();

        QueryWrapper<GreenWave> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        List<GreenWave> greenWaveList = greenWaveMapper.selectList(queryWrapper);

        Integer usingNumber = greenWaveMapper.getAllUsingBandNumber();
        Integer notUsingNumber = greenWaveMapper.getAllNotUsingBandNumber();

        if (usingNumber == null) {
            usingNumber = 0;
        }
        if (notUsingNumber == null) {
            notUsingNumber = 0;
        }

        resAllGreenWave.setGreenWaveList(greenWaveList);
        resAllGreenWave.setAllNumber(greenWaveList.size());
        resAllGreenWave.setUsingNumber(usingNumber);
        resAllGreenWave.setNotUsingNumber(notUsingNumber);

        return resAllGreenWave;
    }


    /**
     * 通过id获取单个绿波带
     *
     * @param id
     * @return
     */
    @Override
    public GreenWave getSingleGreenWave(Long id) {
        GreenWave greenWave = greenWaveMapper.selectById(id);
        return greenWave;
    }


    /**
     * 修改绿波带状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateGreenWaveStatus(Long id, int status) {
        UpdateWrapper<GreenWave> updateWrapper = new UpdateWrapper<>();
        GreenWave greenWave = greenWaveMapper.selectById(id);
        updateWrapper.eq("green_wave_id", id);
        updateWrapper.set("status", status);
        return greenWaveMapper.update(greenWave, updateWrapper);
    }

    /**
     * 添加新的绿波带
     *
     * @param reqGreenWaveAndCross
     */
    @Override
    public void addGreenWave(ReqGreenWaveAndCross reqGreenWaveAndCross) {
        List<GreenWaveCross> reqGreenWaveCrosseList = reqGreenWaveAndCross.getGreenWaveCrossList();

        for (GreenWaveCross greenWaveCross : reqGreenWaveCrosseList) {
            greenWaveCrossMapper.insert(greenWaveCross);
        }
        greenWaveMapper.addGreenWave(reqGreenWaveAndCross.getGreenWave());
    }

    /**
     * 获取绿波带详情
     *
     * @param greenWaveId
     * @return
     */
    @Override
    public ResGreenWaveAndCross getGreenWaveDetail(Long greenWaveId) {
        ResGreenWaveAndCross resGreenWaveAndCross = new ResGreenWaveAndCross();

        resGreenWaveAndCross.setGreenWave(getSingleGreenWave(greenWaveId));
        resGreenWaveAndCross.setGreenWaveCrossList(greenWaveCrossMapper.getCrossListByGreenWaveId(greenWaveId));

        return resGreenWaveAndCross;
    }

    /**
     * 删除绿波带
     *
     * @param greenWaveId
     * @return
     */
    @Override
    public int deleteGreenWave(Long greenWaveId) {
        int res1 = greenWaveMapper.deleteById(greenWaveId);
        if (res1 == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改绿波带
     *
     * @param greenWave
     */
    @Override
    public int updateGreenWave(GreenWave greenWave) {
        return greenWaveMapper.updateById(greenWave);
    }

    /**
     * 添加和更新绿波带内的路口信息
     *
     * @param greenWaveId
     * @param greenWaveCross
     */
    @Override
    public void addAndUpdateGreenWaveCross(Long greenWaveId, GreenWaveCross greenWaveCross) {
        greenWaveCrossMapper.addGreenWaveCross(greenWaveCross, greenWaveId);
    }

    /**
     * 删除绿波带下的路口信息
     *
     * @param greenWaveId
     * @param greenWaveCrossId
     * @return
     */
    @Override
    public int deleteGreenWaveCross(Long greenWaveId, Long greenWaveCrossId) {
        return greenWaveCrossMapper.deleteGreenWaveCross(greenWaveId, greenWaveCrossId);
    }


}
